package com.yanqu.road.server.cmd.cricket.court;

import com.yanqu.road.logic.model.cricket.CricketUserBaseInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.cricket.CricketModule;
import com.yanqu.road.server.gameplayer.module.cricket.entity.DbUserCricketBattleLog;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = GameProtocol.S_CRICKET_BATTLE_LOG, desc = "战报列表")
public class CricketBattleLogCmd extends AbstractCommand {
    private int type;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CricketProto.CricketBattleLogReqMsg reqMsg = CricketProto.CricketBattleLogReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        CricketProto.CricketBattleLogRespMsg.Builder respMsg = CricketProto.CricketBattleLogRespMsg.newBuilder();
        respMsg.setRet(0);
        if (type == 1) {
            // 热门上跨服拿
            CricketProto.CricketBattleLogReqMsg.Builder reqMsg = CricketProto.CricketBattleLogReqMsg.newBuilder();
            reqMsg.setType(type);
            player.sendPacket(Protocol.C_CRICKET_HOT_BATTLE_LOG_LIST, reqMsg);
        } else {
            for (DbUserCricketBattleLog dbCricketHotBattleLog : player.getModule(CricketModule.class).getCricketBattleLogList()) {
                CricketProto.CricketBattleLogListTemp.Builder temp = CricketProto.CricketBattleLogListTemp.newBuilder();
                temp.setId(dbCricketHotBattleLog.getId());
                for (CricketUserBaseInfo userBaseInfo : dbCricketHotBattleLog.getUserBaseInfoList()) {
                    CricketProto.CricketUserBattleDataTemp cricketUserBattleDataTemp = userBaseInfo.toProtobuf();
                    if (cricketUserBattleDataTemp != null) {
                        temp.addUser(cricketUserBattleDataTemp);
                    }
                }
                if (temp.getUserList().size() < 2) {
                    // 做个容错
                    continue;
                }
                temp.setType(dbCricketHotBattleLog.getBattleType());
                String[] split = dbCricketHotBattleLog.getScore().split(";");

                if (split.length > 0) {
                    temp.setScore(split[0]);
                }
                if (split.length > 1) {
                    String[] split1 = split[1].split(":");
                    temp.addAddRankScore(Integer.parseInt(split1[0]));
                    temp.addAddRankScore(Integer.parseInt(split1[1]));
                }

                temp.setTimestamp(dbCricketHotBattleLog.getBattleTimestamp());
                respMsg.addList(temp);
            }
            player.sendPacket(ClientProtocol.U_CRICKET_BATTLE_LOG, respMsg);
        }


    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
